//
//  HPReprintView.swift
//  MyWeibo
//
//  Created by apple on 16/4/15.
//  Copyright © 2016年 Apengpeng. All rights reserved.
//

import UIKit
import SnapKit

class HPReprintView: UIView {
    
    var bottomCons: Constraint?
    
    // 拥有数据模型, 在didSet 方法中绑定数据
    var statusCellVM: HPStatusCellViewModel? {
        didSet{
            contentLabel.text = statusCellVM?.status?.retweeted_status?.text
            
            // 判断转发微博是否有图片
            // 卸载约束
            self.bottomCons?.uninstall()
            
            if let urls = statusCellVM?.status?.retweeted_status?.pic_urls where urls.count > 0{
                // 设置数据源
                statusPictureView.imageURLs = urls
                // 设置显示
                statusPictureView.hidden = false
                
                // 更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(statusPictureView.snp_bottom).constraint
                })
                    
            } else {
                
                statusPictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
                // 置空 模型 数组
                statusPictureView.imageURLs = nil
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 懒加载微博内容控件
    private lazy var contentLabel: UILabel = UILabel(text: "转发文本内容", textColor: UIColor.darkGrayColor(), fontSzie: 14, margin: homePageCellMargin)
    
//    private lazy var contentLabel: UILabel = {
//       
//        let rePrintLabel = UILabel(text: "转发文本内容", textColor: UIColor.darkGrayColor(), fontSzie: 14)
//        rePrintLabel.backgroundColor = UIColor.whiteColor()
//        return rePrintLabel
//    }()
    
    private lazy var statusPictureView: HPStatusPictureView = HPStatusPictureView()
}



// 转发微博的视图处理
extension HPReprintView {
    
    private func setupUI() {
     
//        backgroundColor = UIColor(white: 0.8, alpha: 1.0)
        backgroundColor = UIColor.whiteColor()
        
        //添加控件
        addSubview(contentLabel)
        addSubview(statusPictureView)
        
        // 设置约束
        contentLabel.snp_makeConstraints { (make) -> Void in
//            make.top.left.equalTo(self).offset(homePageCellMargin)
            
            make.left.equalTo(self.snp_left).offset(homePageCellMargin)
            make.top.equalTo(self.snp_top)
        }
        
        // 设置约束
        statusPictureView.snp_makeConstraints { (make) -> Void in
            
//             以contentLabel 为参考
//            make.left.equalTo(contentLabel.snp_left)
            /* 错误约束, 因为contentLabel 已经有了边距了
//            make.left.equalTo(contentLabel.snp_left).offset(homePageCellMargin)
            */
            
            make.left.equalTo(self.snp_left).offset(homePageCellMargin)
            make.top.equalTo(contentLabel.snp_bottom)
            // 设置图片的大小尺寸
            make.size.equalTo(CGSize(width: 100, height: 150))
        }
        
        
        // 内容的view 自动计算高度, 约束底部 根据服务器返回的数据
        self.snp_makeConstraints { (make) -> Void in
           self.bottomCons = make.bottom.equalTo(statusPictureView.snp_bottom).constraint
        }
    }
}